<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>游记明细</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css">
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/travelContent.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <script src="/js/common.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>


    <style>
        .usefulClick {
            color: #F32D27;
        }
    </style>

    <script>
        $(function () {
            //获取地址栏上的id值
            var params = getParams();
            if (params.id) {
                //全部游记点击某一篇游记数据回显
                $.get("/travels/" + params.id, function (data) {
                    //游记作者已发布篇数的回显
                    var authorId = data.data.author.id;
                    $.ajax({
                        type: "GET",
                        url: "/users/" + authorId + "/travelNums",
                        success: function (data) {
                            $("#travelNums").html(data.travelNums)
                        }
                    })

                    //评论按钮绑定作者的ID
                    $("#commentBtn").click(function () {
                        window.parent.location.href = "/mine/travelComment.html?id="+params.id;
                    })


                    //游记数据的回显
                    $(".travels").renderValues(data.data, {
                        setPerson: function (ele, val) {
                            var html = "";
                            if (val == 1) {
                                html = "一个人的旅行";
                            } else if (val == 2) {
                                html = "和父母";
                            } else if (val == 3) {
                                html = "和朋友";
                            } else if (val == 4) {
                                html = "和同事";
                            } else if (val == 5) {
                                html = "和爱人";
                            } else {
                                html = "和同事";
                            }
                            $(ele).html(html);
                        }
                    });


                })
                //异步请求获取游记的收藏次数和点赞次数
                getLikeAndCollectorNums(params.id);

                $.get("/travels/"+params.id+"/comment",function (data) {
                    $("#commentBtn").addClass("usefulClick");
                    //文章评论的次数
                    $(".comment_num").html(data.total);
                    //个人用户回复次数
                    $("#replyNum").html(data.total)
                })


            }


            var userStr = sessionStorage.getItem("user");
            var userJSON = JSON.parse(userStr);


            if (userJSON) {
                //用户如果已经登录并且点评过该文章 为点赞按钮加上样式
                $.get("/users/" + userJSON.id + "/travels/" + params.id + "/travelConcerns", function (data) {
                    if (data.success) {
                        $("#likeBtn").addClass('usefulClick');
                    }
                })
            }

            //点赞按钮
            $("#likeBtn").click(function () {
                //判断用户是否登录
                if (!userJSON) {
                    login("亲,请先登录再点赞");
                } else {
                    //用户已经登录点赞逻辑
                    //console.log(userJSON.id);
                    //如果点赞按钮加上样式 表示当前用户已经点赞 再一次点击去除样式并且让点赞数量-1
                    if ($(this).hasClass('usefulClick')) {
                        //游记点赞的取消
                        $.ajax({
                            type: "PUT",
                            url: "/travelConcerns",
                            data: {"liker.id": userJSON.id, "travel.id": params.id},
                            success: function (data) {
                                if (data.success) {
                                    $(document).dialog({
                                        type: 'toast',
                                        infoIcon: '/imgs/fail.png',
                                        infoText: '取消点赞',
                                        autoClose: 2500
                                    });
                                    $("#likeBtn").removeClass('usefulClick')
                                    //异步请求获取游记的收藏次数和点赞次数
                                    getLikeAndCollectorNums(params.id);
                                }
                            }
                        })
                    } else {
                        //如果没有加上样式 表示当前用户还没对该游记进行点赞 点击让点赞数量+1 并回显数字
                        //新增点赞需要判断 当前用户是否已经存在点评中
                        //如果在存在 在表的字段上直接更新即可
                        //如果不存在 在评论表插入新的评论
                        $.ajax({
                            type: "POST",
                            url: "/travelConcerns",
                            data: {"liker.id": userJSON.id, "travel.id": params.id},
                            success: function (data) {
                                if (data.success) {
                                    $(document).dialog({
                                        type: 'toast',
                                        infoIcon: '/imgs/success.png',
                                        infoText: '成功点赞',
                                        autoClose: 2500
                                    });
                                    $("#likeBtn").addClass('usefulClick');
                                    //异步请求获取游记的收藏次数和点赞次数
                                    getLikeAndCollectorNums(params.id);
                                }
                            }
                        })
                    }
                }
            })
            //--------------------------------收藏按钮-----------------------------------------------
            if (userJSON) {
                //用户如果已经登录并且点评过该文章 为点赞按钮加上样式
                $.get("/users/" + userJSON.id + "/travels/" + params.id + "/concernCollector", function (data) {
                    if (data.success) {
                        $("#collectBtn").addClass('usefulClick');
                    }
                })
            }

            //收藏按钮
            $("#collectBtn").click(function () {
                //判断用户是否登录
                if (!userJSON) {
                    login("亲,请先登录再收藏");
                } else {
                    if ($(this).hasClass('usefulClick')) {
                        //游记收藏的取消
                        $.ajax({
                            type: "PUT",
                            url: "/travelConcerns/Collector",
                            data: {"collector.id": userJSON.id, "travel.id": params.id},
                            success: function (data) {
                                if (data.success) {
                                    $(document).dialog({
                                        type: 'toast',
                                        infoIcon: '/imgs/fail.png',
                                        infoText: '取消收藏',
                                        autoClose: 2500
                                    });
                                    $("#collectBtn").removeClass('usefulClick')
                                    //异步请求获取游记的收藏次数和点赞次数
                                    getLikeAndCollectorNums(params.id);
                                }
                            }
                        })
                    } else {
                        //如果没有加上样式 表示当前用户还没对该游记进行收藏
                        $.ajax({
                            type: "POST",
                            url: "/travelConcerns/Collector",
                            data: {"collector.id": userJSON.id, "travel.id": params.id},
                            success: function (data) {
                                if (data.success) {
                                    $(document).dialog({
                                        type: 'toast',
                                        infoIcon: '/imgs/success.png',
                                        infoText: '收藏成功',
                                        autoClose: 2500
                                    });
                                    $("#collectBtn").addClass('usefulClick');
                                    //异步请求获取游记的收藏次数和点赞次数
                                    getLikeAndCollectorNums(params.id);
                                }
                            }
                        })
                    }

                }
            })
        })

    </script>
</head>


<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col">
            <a href="javascript:history.go(-1);">
                <span><i class="fa fa-chevron-left"></i></span>
            </a>
        </div>
        <div class="col">
        </div>
    </div>
</div>

<div class="travels">
    <img class="cover" render-src="coverUrl">
    <div class="container">
        <h6 class="title" render-html="title"></h6>
        <small>
            <!--发布时间-->
            <span render-html="releaseTime"></span>
            浏览 <span> 0</span>
            回复 <span id="replyNum"> 0</span>
        </small>
        <div class="row">
            <div class="col-2 img">
                <img class="rounded-circle" render-src="author.headImgUrl">
            </div>
            <div class="col-6 mine">
                <small>
                    <span render-html="author.nickName">喵喵</span>
                    <button class="btn" id="followBtn">关注</button>
                    <br>
                    <span id="travelNums"></span>篇游记
                    <span>0 </span>粉丝
                </small>
            </div>
            <div class="attention col">
                <span class="addr" render-html="place.name"></span>
            </div>
        </div>
    </div>

    <div class="container">
        <fieldset>
            <legend>我的出行信息</legend>
            <div class="d-flex">
                <div class="p-2  flex-fill">
                    <i class="fa fa-calendar"></i> 出发时间 <span render-html="travelTime"></span>
                </div>
                <div class="p-2  flex-fill"><i class="fa fa-money"></i> 人均费用
                    <span render-html="perExpends">120</span></div>
            </div>
            <div class="d-flex">
                <div class="p-2  flex-fill">
                    <i class="fa fa-user-circle"></i> 人物
                    <span render-fun="setPerson" render-key="person">和朋友</span>
                </div>
                <div class="p-2  flex-fill"><i class="fa fa-clock-o"></i> 出行天数
                    <span render-html="days"></span>
                </div>
            </div>
        </fieldset>

        <div class="content" render-html="travelContent.content">

        </div>
    </div>

    <hr>


</div>


<div class="container d-flex">

    <!--点赞按钮-->
    <div class="p-2  flex-fill">
        <i class="fa fa-thumbs-o-up" id="likeBtn">
            <span class="like_num"></span>
        </i>
    </div>

    <!--评论按钮-->
    <div class="p-2  flex-fill"><i class="fa  fa-commenting-o" id="commentBtn">
        <span class="comment_num"></span>
    </i>
    </div>

    <!--收藏按钮-->
    <div class="p-2  flex-fill"><i class="fa fa-star-o" id="collectBtn">
        <span class="collect_num"></span>
    </i>
    </div>
</div>


</body>
    <script>


    </script>


</html>